package com.cc.main.cmd;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.cc.main.Const;
import com.cc.main.ICommonCmd;
import com.cc.main.problems.bean.ProblemBean;
import com.cc.main.util.MyUtils;

/**
 * settime指令
 * @author TR
 *
 */
public class SettimeCmd implements ICommonCmd{
	private String startTagTime = "";
	private String endTagTime = "";
	
	@Override
	public void execute(Object action) {
		// TODO Auto-generated method stub
		System.out.println("请输入时间范围（MMDD/HHMM/MMDDHHMM）。如07010934-07021056");
		int noInputCounter = 0;
		while(true) {
			String input = MyUtils.getInput(Const.MSettimeCmd);
			if(input.isEmpty()) {
				noInputCounter++;
				//连续两次无输入，直接清空设置的time
				if(noInputCounter == 2) {
					startTagTime = "";
					endTagTime = "";
					break;
				}else {
					System.out.println("本次没有输入内容，再一次将退出并清空设置时间!");
					continue;
				}
			}
			noInputCounter = 0;
			String[] ss = input.split("-");
			if(ss.length != 2) {
				System.out.println("时间范围格式不正确（未检测到‘-’），请重新输入!");
				continue;
			}
			if(!MyUtils.isNumeric(ss[0]) || !MyUtils.isNumeric(ss[1])){
				System.out.println("时间范围格式不正确(非数字格式)，请重新输入!");
				continue;
			}
			if(ss[0].length()%4 != 0 || ss[1].length()%4 != 0){
				System.out.println("时间范围格式不正确(4或8位数字)，请重新输入!");
				continue;
			}
			if(ss[0].compareTo(ss[1]) >= 0) {
				System.out.println("起始时间>=结束时间，请重新输入!");
				continue;
			}
			distinguishMMDDOrHHMMOrMMDDHHMM(ss[0],ss[1]);
			System.out.println("你设置的时间为:" + startTagTime + " ~ " + endTagTime);
			break;
		}
		
		ProblemBean.getInstance().timeList.clear();
		ProblemBean.getInstance().timeList.add(0, startTagTime);
		ProblemBean.getInstance().timeList.add(1, endTagTime);
	}
	
	@Override
	public String printCurrentEnv() {
		// TODO Auto-generated method stub
		if(startTagTime.isEmpty()) {
			return Const.MSettimeCmd + ":";
		}
		return Const.MSettimeCmd + ":" + startTagTime + " ~ " + endTagTime;
	}
	
	/**
	 * 0701093410 转 07-01 09：34：10
	 * @param t
	 * @return
	 */
	private void distinguishMMDDOrHHMMOrMMDDHHMM(String st,String et) {
		
		String mSt = st;
		String mEt = et;
		//自动格式识别
		if(mSt.length() == 4 && mEt.length() == 4) {
		
			Date date = new Date();
			SimpleDateFormat sf = new SimpleDateFormat("MMdd");
			String currentTime = sf.format(date);
			
//			String hValue = et.substring(0,2);
//			String lValue = et.substring(2);
			//月份+日期格式
//			if(hValue.compareTo("01") >=0 && hValue.compareTo("12") <= 0 &&
//					lValue.compareTo("01") >=0 && lValue.compareTo("31") <= 0) {
			if(isMonthFormat(st) && isMonthFormat(et)) {
				
			} else {
				//小时 + 分钟 格式
				mSt = currentTime + st;
				mEt = currentTime + et;
			}
		}
		startTagTime = formatDate(mSt);
		endTagTime = formatDate(mEt);
	}
	private String formatDate(String t) {
		String cont = "";
		for(int i=0;i<10;i++) {
			if(i < t.length()) {
				cont += t.charAt(i);
			}else {
				cont += "0";
			}
			if(i == 1) {
				cont += "-";
			}else if(i == 3) {
				cont += " ";
			}else if(i == 5 || i == 7) {
				cont += ":";
			}else if(i == 9) {
				cont += ".";
			}
		}
		return cont;
	}
	
	private boolean isMonthFormat(String t) {
		String hValue = t.substring(0,2);
		String lValue = t.substring(2);
		return (hValue.compareTo("01") >=0 && hValue.compareTo("12") <= 0 &&
				lValue.compareTo("01") >=0 && lValue.compareTo("31") <= 0);
	}

}
